/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.util.Properties;

/**
 *
 * @author BentH
 */
public class DBConnectionManager
{
    private static final String CONFIG_FILE = "Database/SongsDB.cfg";
    private static DBConnectionManager instance = null;
    private SQLServerDataSource ds;
    
    private DBConnectionManager() throws IOException
    {
        setUpDataSource();
    }
    
    public static DBConnectionManager getInstance() throws IOException
    {
        if (instance == null)
            instance = new DBConnectionManager();
        return instance;
    }

    private void setUpDataSource() throws IOException
    {
        Properties props = new Properties();
        try (FileReader fr = new FileReader(CONFIG_FILE))
        {
            props.load(fr);
            ds.setServerName(props.getProperty("SERVER"));
            ds.setPortNumber(Integer.parseInt(props.getProperty("PORT")));
            ds.setDatabaseName(props.getProperty("DATABASE"));
            ds.setUser(props.getProperty("USER"));
            ds.setPassword(props.getProperty("PASSWORD"));
        }
    }
    
     public Connection getConnection() throws SQLServerException
     {
         return ds.getConnection();
     }
}
